<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true">
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="banners" highlight-current-row v-loading="listLoading" style="width: 100%;">
			<el-table-column type="index" width="60"></el-table-column>
			<el-table-column prop="name" label="名称" sortable></el-table-column>
            <el-table-column property="path" label="图片" align="center">
                <template slot-scope="scope">
                    <el-image
                        style="width: 150px; height: auto;"
                        :src="scope.row.image_url"
                        :preview-src-list="[scope.row.image_url]">
                    >
                    </el-image>
                </template>
            </el-table-column>
            <el-table-column prop="link" label="链接"></el-table-column>
			<el-table-column prop="description" label="描述"></el-table-column>
			<el-table-column label="操作">
				<template slot-scope="scope">
                    <el-button-group>
                        <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                        <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
                    </el-button-group>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="20" :total="total" style="float:right;">
			</el-pagination>
		</el-col>

		<!--编辑界面-->
		<el-dialog title="编辑" :visible.sync="editFormVisible" :close-on-click-modal="false">
			<el-form :model="editForm" label-width="120px" :rules="editFormRules" ref="editForm">
				<el-form-item label="幻灯片名称" prop="name">
					<el-input v-model="editForm.name" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="幻灯片">
                    <el-upload
                        name="file"
                        :limit="1"
                        class="upload-demo"
                        action="admin/upload/image_upload"
                        :auto-upload="true"
                        :multiple="false"
                        :data="{'type': 'banner'}"
                        :on-remove="handleRemove"
                        :on-success="uploadSuccess"
                        :on-preview="handlePictureCardPreview"
                        :file-list="fileList"
                        list-type="picture">
                        <el-button size="small" type="primary">点击上传</el-button>
                    </el-upload>
                    <el-dialog :visible.sync="dialogVisible">
                        <img width="100%" :src="dialogImageUrl" alt="">
                    </el-dialog>
				</el-form-item>
				<el-form-item label="链接">
					<el-input v-model="editForm.link"></el-input>
				</el-form-item>
				<el-form-item label="描述">
					<el-input type="textarea" v-model="editForm.description"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="editFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
			</div>
		</el-dialog>

		<!--新增界面-->
        <el-dialog title="新增" :visible.sync="addFormVisible" :close-on-click-modal="false">
            <el-form :model="addForm" label-width="120px" :rules="addFormRules" ref="addForm">
                <el-form-item label="幻灯片名称" prop="name">
                    <el-input v-model="addForm.name" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="幻灯片">
                    <el-upload
                        name="file"
                        :limit="1"
                        class="upload-demo"
                        action="admin/upload/image_upload"
                        :auto-upload="true"
                        :multiple="false"
                        :data="{'type': 'banner'}"
                        :file-list="fileList"
                        :on-remove="handleAddRemove"
                        :on-success="uploadAddSuccess"
                        :on-preview="handlePictureCardPreview"
                        list-type="picture">
                        <el-button size="small" type="primary">点击上传</el-button>
                    </el-upload>
                    <el-dialog :visible.sync="dialogVisible">
                        <img width="100%" :src="dialogImageUrl" alt="">
                    </el-dialog>
                </el-form-item>
                <el-form-item label="链接">
                    <el-input v-model="addForm.link"></el-input>
                </el-form-item>
                <el-form-item label="描述">
                    <el-input type="textarea" v-model="addForm.description"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="addFormVisible = false">取消</el-button>
                <el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
            </div>
        </el-dialog>
	</section>
</template>

<script>
    import {addBanner, editBanner, getBanner, removeBanner, removeFile} from '../../api/api';

	export default {
		data() {
			return {
                fileList: [],
                c_image:{},
                dialogImageUrl: '',
                dialogVisible: false,
                disabled: false,
                banners: [],
				total: 0,
				page: 1,
				listLoading: false,
				editFormVisible: false,//编辑界面是否显示
				editLoading: false,
				editFormRules: {
					name: [
						{ required: true, message: '请输入姓名', trigger: 'blur' }
					]
				},
				//编辑界面数据
				editForm: {
					name: '',
					image_url: '',
					description: '',
					link: '',
				},
				addFormVisible: false,//新增界面是否显示
				addLoading: false,
				addFormRules: {
					name: [
						{ required: true, message: '请输入幻灯片名称', trigger: 'blur' }
					]
				},
				//新增界面数据
				addForm: {
                    name: '',
                    image_url: '',
                    description: '',
                    link: '',
				}
			}
		},
		methods: {
            handleRemove(file) {
                var file_name;
                if(file.response !== undefined){
                    file_name = file.response.data;
                }else{
                    file_name = file.url;
                }
                var data = {'file_name': file_name};
                removeFile(data).then((res) => {
                    if(res.code == 200){
                        this.editForm.image_url = ''
                    }
                })
            },
            handleAddRemove(file) {
                var file_name = file.response.data;
                var data = {'file_name': file_name};
                removeFile(data).then((res) => {
                    if(res.code == 200){
                        this.addForm.image_url = ''
                    }
                })
            },
            uploadSuccess(res){
                this.editForm.image_url = res.data
            },
            uploadAddSuccess(res){
                this.addForm.image_url = res.data
            },
            handlePictureCardPreview(file) {
                this.dialogImageUrl = file.url;
                this.dialogVisible = true;
            },
			handleCurrentChange(val) {
				this.page = val;
				this.get_banners();
			},
			//获取幻灯片列表
            get_banners() {
				let para = {
					page: this.page,
				};
				this.listLoading = true;
				getBanner(para).then((res) => {
					this.banners = res.data.data;
					this.listLoading = false;
				});
			},
			//删除
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					let para = { banner_id: row.id };
					removeBanner(para).then((res) => {
					    if (res.success){
                            this.listLoading = false;
                            this.$message({
                                message: res.msg,
                                type: 'success'
                            });
                            this.get_banners();
                        }else {
                            this.$message({
                                message: res.msg,
                                type: 'fail'
                            });
                        }
					});
				}).catch(() => {
                    this.$message({
                        message: '删除失败',
                        type: 'fail'
                    });
				});
			},

			//显示编辑界面
			handleEdit: function (index, row) {
				this.editFormVisible = true;
                this.editForm = Object.assign({}, row);
                this.fileList = [{'name': row.name, 'url': row.image_url}]
			},
			//显示新增界面
			handleAdd: function () {
				this.addFormVisible = true;
                this.fileList = []
			},
			//编辑
			editSubmit: function () {
				this.$refs.editForm.validate((valid) => {
					if (valid) {
					    console.log(this.editForm);
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.editLoading = true;
							let para = Object.assign({}, this.editForm);
							editBanner(para).then((res) => {
								this.editLoading = false;
								this.$message({
									message: '提交成功',
									type: 'success'
								});
								this.$refs['editForm'].resetFields();
								this.editFormVisible = false;
								this.get_banners();
							});
						});
					}
				});
			},
			//新增
			addSubmit: function () {
				this.$refs.addForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.addLoading = true;
							addBanner(this.addForm).then((res) => {
								this.addLoading = false;
								this.$message({
									message: '提交成功',
									type: 'success'
								});
								this.$refs['addForm'].resetFields();
								this.addFormVisible = false;
								this.get_banners();
							});
						});
					}
				});
			}
		},
		mounted() {
			this.get_banners();
		}
	}

</script>

<style scoped>
    .originImg{
        width: 100%;
    }
</style>
